﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO.Ports;
using System.Windows.Forms;

namespace CarBlackBox
{
    public partial class SerialCommunication
    {
#if ComDebug
        public WFNetLib.Forms.CommunicationDebugForm CommunicationDebugForm;
#endif        
        public static string DefaultPortName=Properties.Settings.Default.DefaultPortName;
        private SerialPort _COM = null;
        private string _portName;
        public SerialCommunication(string portName, IWin32Window owner)
        {
            _portName = portName;
#if ComDebug
            CommunicationDebugForm=new WFNetLib.Forms.CommunicationDebugForm();
            CommunicationDebugForm.Show(owner);
#endif
        }
        public SerialCommunication(IWin32Window owner)
        {            
            _portName = string.Empty;
#if ComDebug
            CommunicationDebugForm = new WFNetLib.Forms.CommunicationDebugForm();
            CommunicationDebugForm.Show(owner);
#endif
        }
        private Boolean OpenSerialPort()
        {
            if (_COM != null && _COM.IsOpen)
            {
                return true;
            }
            if(string.IsNullOrEmpty(_portName))//没有临时串口号，使用默认值
                _COM = new SerialPort(DefaultPortName, 9600, Parity.Odd,8,StopBits.One);
            else
                _COM = new SerialPort(_portName, 9600, Parity.Odd, 8, StopBits.One);
            _COM.ReadTimeout = Properties.Settings.Default.SerialPortTimeOut;
            _COM.Close();
            while (true)
            {
                try
                {
                    _COM.Open();
                    break;
                }
                catch (Exception ex)
                {
                    if (MessageBox.Show(ex.Message, "Waring", MessageBoxButtons.RetryCancel, MessageBoxIcon.Question) == DialogResult.Cancel)
                    {
                        WFNetLib.Log.SystemLogData log = new WFNetLib.Log.SystemLogData();
                        log.LogType = LogType.串口通信.ToString();
                        log.LogContent = "系统提示:" + ex.Message;
                        log.LogRemark = "串口无法打开";
                        log.InserSQLLog();
                        MessageBox.Show("通信失败，指定串口无法打开!!");
                        return false;
                    }
                }
            }
            return true;
        }
        private void CloseSerialPort()
        {
            _COM.Close();
            _COM.Dispose();
        }
        public static string ResultMessage(RS485Result ret)
        {
            switch (ret)
            {
                case RS485Result.Close:
                    return "通信失败，指定串口无法打开!!";
                case RS485Result.Timeout:
                    return "通信失败，记录仪没有回应!!";
                case RS485Result.Error:
                    return "通信失败，记录仪回应数据有误!!";
                case RS485Result.OK:
                    return "通信成功";
            }
            return "未知通信失败原因";
        }
    }
}
